// import shallowEqual from "shared/shallowEqual";

// 1. 在react/src/ReactBaseClasses中

// React.Component
function Component() {}

function ComponentDummy() {}
ComponentDummy.prototype = Component.prototype;

function PureComponent() {}
// 原型链继承
// @ts-ignore
const pureComponentPrototype = (PureComponent.prototype = new ComponentDummy());
pureComponentPrototype.constructor = PureComponent;
// assign(pureComponentPrototype, Component.prototype);
pureComponentPrototype.isPureReactComponent = true;

// 2. 在类组件更新的阶段 检查是否需要更新 进行浅比较
// react-reconciler/src/ReactFiverClassComponent
function checkShouldComponentUpdate(
  workInProgress: any,
  ctor: any,
  oldProps: any,
  newProps: any,
  oldState: any,
  newState: any,
  nextContext: any,
) {
  // pureComponent的优化
  if (ctor.prototype && ctor.prototype.isPureReactComponent) {
    return (
      // 浅比较
      !shallowEqual(oldProps, newProps) || !shallowEqual(oldState, newState)
    );
  }
}

function shallowEqual(objA: any, objB: any) {
  if (Object.is(objA, objB)) {
    return true;
  }

  if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) {
    return false;
  }

  const keysA = Object.keys(objA);
  const keysB = Object.keys(objB);

  if (keysA.length !== keysB.length) {
    return false;
  }

  // Test for A's keys different from B.
  for (let i = 0; i < keysA.length; i++) {
    const currentKey = keysA[i];
    if (
      !Object.prototype.hasOwnProperty.call(objB, currentKey) ||
      !Object.is(objA[currentKey], objB[currentKey])
    ) {
      return false;
    }
  }

  return true;
}
